package cn.com.scitc.login;

import cn.com.scitc.dao.ConnDB;
import cn.com.scitc.main.Main;
import cn.com.scitc.publicModule.ShowPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Login extends JFrame implements ActionListener {

    private JPanel jPanel;
    private JLabel logo;
    private JLabel accountText, passwordText;
    private JTextField accountInput, passwordInput;
    private JButton loginBtn;

    private Connection conn;

    /**
     * 构造函数
     */
    public Login() {
        conn = ConnDB.openConn();
        jPanel = new JPanel(null);

        logo = new JLabel();
        int width = 130, height = 130;
        logo.setBounds(77 + 100, 0, width, height);
        ImageIcon icon = new ImageIcon("src/main/java/cn/com/scitc/images/CubeLibrary-1.gif");
        icon.setImage(icon.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));
        logo.setIcon(icon);
        jPanel.add(logo);

        passwordText = new JLabel("账号:");
        passwordText.setHorizontalAlignment(SwingConstants.RIGHT);
        passwordText.setBounds(77, 86 + 30, 85, 16);
        jPanel.add(passwordText);

        accountText = new JLabel("密码:");
        accountText.setHorizontalAlignment(SwingConstants.RIGHT);
        accountText.setBounds(77, 128 + 30, 85, 16);
        jPanel.add(accountText);

        accountInput = new JTextField();
        accountInput.setBounds(174, 83 + 30, 205, 26);
        accountInput.setColumns(10);
        jPanel.add(accountInput);
        accountInput.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                if (e.getExtendedKeyCode() == KeyEvent.VK_ENTER) {
                    passwordInput.requestFocus();
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {

            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });

        passwordInput = new JPasswordField();
        passwordInput.setBounds(174, 123 + 30, 205, 26);
        passwordInput.setColumns(10);
        jPanel.add(passwordInput);
        passwordInput.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                if (e.getExtendedKeyCode() == 10) {
                    loginBtn.doClick();
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {

            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });

        loginBtn = new JButton("登录");
        loginBtn.setBounds(177, 200, 200, 26);
        jPanel.add(loginBtn);
        loginBtn.addActionListener(this);

        jPanel.setBackground(Color.white);

        new ShowPanel(this, jPanel, "立方库智能停车管理系统", 490, 310, false);
        accountInput.setText("511521199807076171");
        passwordInput.setText("123");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(loginBtn)) {
            if (accountInput.getText().trim().isEmpty() || passwordInput.getText().trim().isEmpty()) {
                JOptionPane.showMessageDialog(this, "账号或密码不能为空", "立方库智能停车管理系统", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            PreparedStatement prep;
            ResultSet res;
            String sql = "select * from Administrator where IDNumber = ? and Password = ?";
            List<String> list = new ArrayList<>();
            try {
                prep = conn.prepareStatement(sql);
                prep.setString(1, accountInput.getText());
                prep.setString(2, passwordInput.getText());
                res = prep.executeQuery();
                if (res.next()) {
                    list.add(res.getString("Name"));
                    list.add(res.getString("AdminiRep"));
                } else {
                    JOptionPane.showMessageDialog(this, "账号或密码有误", "立方库智能停车管理系统", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            new Main(list.get(0), list.get(1));
            dispose();
        }
    }
}
